﻿using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace FinanceManager
{
    public class IniReader
    {
        public string inipath;
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public IniReader(string INIPath)
        {
            inipath = INIPath;
        }

        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.inipath);
        }

        #region 读数据
        public string IniReadValue(string Section, string Key)
        {
            if (ExistINIFile())
            {
                StringBuilder temp = new StringBuilder(500);
                int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
                return temp.ToString();
            }
            else {
                return null;
            }
        } 
        #endregion

        public bool ExistINIFile()
        {
            return File.Exists(inipath);
        }
    }
}
